package com.vcc.boss.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.TaskLog;
import com.vcc.service.boss.response.taskLog.BossTaskLogInfoVo;
import com.vcc.service.boss.response.taskLog.BossTaskLogVoBoss;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.BossUserService;
import com.vcc.service.service.TaskLogInfoService;
import com.vcc.service.service.TaskLogService;
import com.vcc.service.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("taskLog")
public class TaskLogController {

    @Resource
    private UserService userService;
    @Resource
    private TaskLogService taskLogService;
    @Resource
    private TaskLogInfoService taskLogInfoService;
    @Resource
    private BossUserService bossUserService;


    @RestPack
    @GetMapping("paging")
    @SaCheckPermission("card:task-log:list:view")
    public Page<BossTaskLogVoBoss> paging(
            Integer pageNumber,
            Integer pageSize,
            String orderId,
            Long targetUserId,
            Integer status,
            String startDate,
            String endDate,
            Integer useFlag
    ) {
        if (targetUserId != null) {
            Long dealerId = userService.getDealerIdById(targetUserId);
            Long dealerParentId = bossUserService.getParentIdById(dealerId);
            // 只能查看自己及下级用户的数据
            if (!BossUserUtil.hasAllData() && (!BossUserUtil.getId().equals(dealerId) || !BossUserUtil.getId().equals(dealerParentId))) {
                throw new ProgramException("非法操作");
            }
        }
        return taskLogService.bossPaging(pageNumber, pageSize, orderId, targetUserId, status, startDate, endDate,useFlag);
    }

    @RestPack
    @GetMapping("infoPaging")
    @SaCheckPermission("card:task-log:list:view")
    public Page<BossTaskLogInfoVo> infoPaging(
            Integer pageNumber,
            Integer pageSize,
            Long taskId
    ) {
        TaskLog taskLog = taskLogService.getByIdOpt(taskId).orElseThrow(() -> new ProgramException("任务不存在"));
        Long dealerId = userService.getDealerIdById(taskLog.getUserId());
        Long dealerParentId = bossUserService.getParentIdById(dealerId);
        // 只能查看自己及下级用户的数据
        if (!BossUserUtil.hasAllData() && (!BossUserUtil.getId().equals(dealerId) || !BossUserUtil.getId().equals(dealerParentId))) {
            throw new ProgramException("非法操作");
        }
        return taskLogInfoService.bossPaging(pageNumber, pageSize, taskId);
    }

    @RestPack
    @GetMapping("get")
    @SaCheckPermission("card:task-log:list:view")
    public BossTaskLogVoBoss get(
            Long id
    ) {
        BossTaskLogVoBoss bossTaskLogVo = taskLogService.getBossById(id);
        Long dealerId = userService.getDealerIdById(bossTaskLogVo.getUserId());
        Long dealerParentId = bossUserService.getParentIdById(dealerId);
        // 只能查看自己及下级用户的数据
        if (!BossUserUtil.hasAllData() && (!BossUserUtil.getId().equals(dealerId) || !BossUserUtil.getId().equals(dealerParentId))) {
            throw new ProgramException("非法操作");
        }
        return bossTaskLogVo;
    }
}
